package com.darren.euc.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.darren.euc.dao.ClientClassificationDao;
import com.darren.euc.entity.ClientClassification;
import com.darren.euc.entity.PageBean;
import com.darren.euc.service.ClientClassificationService;
import com.darren.euc.util.StringUtil;

import net.sf.json.JSONObject;


@Service
public class ClientClassificationServiceImpl implements ClientClassificationService {
	
	@Autowired
    private ClientClassificationDao clientClassificationDao;
	

    @Override
    public ClientClassification save(ClientClassification clientClassification) {
        return clientClassificationDao.save(clientClassification);
    }

    @Override
    public ClientClassification update(ClientClassification clientClassification) {
        return clientClassificationDao.saveOrUpdate(clientClassification);
    }
    
    
    @Override
    public void delete(ClientClassification clientClassification) {
    	clientClassificationDao.delete(clientClassification);
    }
    
	@Override
	public List<ClientClassification> getAll() {
		return clientClassificationDao.getAll();
	}
	
	
	@SuppressWarnings("rawtypes")
	public PageBean queryForPageUseHQL(JSONObject requestJson,Integer pageSize, Integer currentPage){		
		String HQL = null;
		String likes = null;
		String equals = null;
			List<String> parameters = new ArrayList<String>();
		JSONObject likeJson = requestJson.getJSONObject("like");
		Iterator likeIterator = likeJson.keys();
		while(likeIterator.hasNext()){
			String key = (String) likeIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = likeJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				likes = (likes == null?"": likes + " OR ") +  key + " like ? ";
				parameters.add("%"+value+"%");
			}
		}
		HQL = likes == null?null:(" (" + likes + ") ");		
		JSONObject equalsJson = requestJson.getJSONObject("equals");
        Iterator equalsIterator = equalsJson.keys();        
		while(equalsIterator.hasNext()){
			String key = (String) equalsIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = equalsJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				equals = (equals == null?"": ""+equals + " and ") +  key + " = ? ";
				parameters.add(value);
			}
		}		
		if(HQL != null){
			if(equals != null){
				HQL = HQL + " and " + equals;
			}
		}else{
			HQL = equals;
		}
		HQL = " from ClientClassification " + (HQL == null?"":" where " + HQL) + " order by displaySequence asc";
		return clientClassificationDao.queryForPageByHQL(HQL, pageSize, currentPage,parameters.toArray());
	}

	@Override
	public List<ClientClassification> queryByName(String name) {
		return clientClassificationDao.queryByCondition(" from ClientClassification where clientClassificationName = ? ", name);
	}
	
	
}